HOSTNAME=`hostname`
cd <%=@serverdir%>
sudo cp server.xml /tmp
sed "s/localhost/$HOSTNAME/" </tmp/server.xml > /tmp/server.xml.mod
sudo mv /tmp/server.xml.mod server.xml
sudo rm /tmp/server.xml
JMS_HOSTNAME=<%=@jmshost%>
DB_HOSTNAME=<%=@dbhost%>
current_ip=`/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
current_ip=`echo $current_ip | cut -d\  -f1`

# Process command line arguments
args=($@)
for i in "${!args[@]}"; do 
  key=`echo ${args[$i]} | cut -d= -f1`
  val=`echo ${args[$i]} | cut -d= -f2`

  case "$key" in
  database) 
  IFS='~' read -a db_ip_arr <<< "${val}"
  ;;
  appserver)
  IFS='~' read -a appserver_ip_arr <<< "${val}"
  ;;
  jms)
  IFS='~' read -a jms_ip_arr <<< "${val}"
  ;;
  agent)
  IFS='~' read -a agent_ip_arr <<< "${val}"
  ;;	
  esac 	
done

# Map the database host name
sudo sh -c "echo ${db_ip_arr[0]} $DB_HOSTNAME >>/etc/hosts"

# Map the jms host name
sudo sh -c "echo ${jms_ip_arr[0]} $JMS_HOSTNAME >>/etc/hosts"

# Map the current host name
NEW_HOSTNAME=`hostname`
sudo sh -c "echo ${current_ip} $NEW_HOSTNAME >>/etc/hosts"

hostname=`hostname`

#Change the default app name in newrelic with real hostname
if [ -f "/opt/IBM/newrelic/newrelic.yml"]
then
   sudo sed -i 's/\(app_name:\)\(.*\)/\1'$hostname'/' /opt/IBM/newrelic/newrelic.yml
fi

#start the server
service libertyservice start